//
//  ZCSearchView.swift
//  weibo
//
//  Created by 张城 on 16/7/23.
//  Copyright © 2016年 张城. All rights reserved.
//

import UIKit

class ZCSearchView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 35))
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI() {
        addSubview(button)
        addSubview(textField)
        
        textField.snp_makeConstraints { (make) in
            make.top.leading.bottom.equalTo(self)
            make.trailing.equalTo(self)
        }
        
        button.snp_makeConstraints { (make) in
            make.top.trailing.bottom.equalTo(self)
        }
    }
    
    private lazy var textField: UITextField = {
        let img = UIImageView (image: UIImage (named: "searchbar_textfield_search_icon"))
        img.frame.size = CGSizeMake(35, 35)
        img.contentMode = .Center
        let text_fd = UITextField ()
        text_fd.layer.borderColor = UIColor.orangeColor().CGColor
        text_fd.layer.borderWidth = 2
        text_fd.backgroundColor = UIColor.whiteColor()
        text_fd.placeholder = "上海iOS班"
        text_fd.leftView = img
        text_fd.leftViewMode = .Always
        text_fd.delegate = self
        return text_fd
    }()
    
    private lazy var button: UIButton = {
        let but = UIButton()
        but.setTitle("取消", forState: .Normal)
        but.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        but.titleLabel?.font = UIFont.systemFontOfSize(15)
        but.frame.size.width = 50
        but.addTarget(self, action: #selector(ZCSearchView.cancelClick), forControlEvents: .TouchUpInside)
        return but
    }()
    
    func cancelClick() {
//        print("fwfw")
        self.textField.snp_updateConstraints { (make) in
            make.trailing.equalTo(self)
        }
        self.textField.resignFirstResponder()
        self.textField.text = ""
        UIView.animateWithDuration(0.2) {
            self.layoutIfNeeded()
        }
    }
    
}

extension ZCSearchView : UITextFieldDelegate {
    func textFieldDidBeginEditing(textField: UITextField) {
        
        self.textField.snp_updateConstraints { (make) in
            make.trailing.equalTo(self).offset(-50)
        }
        
        UIView.animateWithDuration(0.2) {
            self.layoutIfNeeded()
        }
    }
}


















